<template>
  <header>
    <div class="header">
      <!-- 头部第一行 -->
      <div class="header-continner">
        <div class="header-use">
          <span>尚品汇欢迎您！</span>
          <span v-if="name">
            <span>{{ name }}</span>
            <button @click="handleLogout">退出登录</button>
          </span>
          <span v-else
            >请 <router-link to="/login">登录</router-link> | 免费
            <router-link to="/register">注册</router-link></span
          >
        </div>
        <ul class="header-nav">
          <li><router-link to="/center">我的订单</router-link></li>
          <li><router-link to="/shopcart">我的购物车</router-link></li>
          <li><router-link to="/">我的尚品汇</router-link></li>
          <li><router-link to="/">尚品汇会员</router-link></li>
          <li><router-link to="/">企业采购</router-link></li>
          <li><router-link to="/">关注尚品汇</router-link></li>
          <li><router-link to="/">合作招商</router-link></li>
          <li><router-link to="/">商家后台</router-link></li>
        </ul>
      </div>
    </div>
    <!-- 头部底座 -->
    <div class="header-bottom header-continner">
      <router-link to="/home"
        ><img src="./imgs/logo.png" alt="尚品汇" class="header-logo"
      /></router-link>
      <form class="header-form" @submit.prevent="toSearch">
        <input type="text" value="" class="header-input" v-model="keyword" />
        <button class="header-btn">搜索</button>
      </form>
    </div>
  </header>
</template>

<script>
import { mapState ,mapActions} from 'vuex'
export default {
  name: 'XHeader',
  methods: {
    ...mapActions('user',['logout']),
    toSearch() {
      // 没有keyword，不需要携带query参数
      const keyword = this.keyword

      // const query = this.$route.query // 直接修改原对象，不行
      const query = {
        ...this.$route.query,
      }
      delete query.keyword // 删除keyword属性

      if (keyword) {
        query.keyword = keyword
      }

      // 问题：重复跳转到同一个地址，会出现一个错误：NavigationDuplicated: Avoided redundant navigation to current location: "/search".
      // 解决：1. this.$router.history.push  2. 无视（因为错误不影响功能）
      this.$router.push({
        name: 'Search',
        query,
      })
    },
    async handleLogout() {
      await this.logout();
      this.$router.push("/");
    }
  },
  data() {
    return {
      keyword: '',
    }
  },
  watch: {
    $route: {
      handler(route) {
        this.keyword = route.query.keyword
      },
      immediate: true,
    },
  },
  computed:{
    ...mapState({
      name:(state)=>{
        return state.user.name
      }
    })
  }
}
</script>

<style scoped>
.header {
  height: 30px;
  background-color: #eaeaea;
}
.header-continner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 1200px;
  height: 100%;
  margin: 0 auto;
}
.header-nav {
  display: flex;
}
.header-nav li {
  border-right: 1px solid #ccc;
  margin-right: 10px;
  padding-right: 5px;
}
.header-nav li:last-child {
  border-right: none;
  margin-right: none;
  padding-right: none;
}
.header-bottom {
  height: 100px;
}
.header-input {
  width: 500px;
  height: 30px;
  border: 2px solid #ea4a36;
  outline: none;
  padding: 0 5px;
}
.header-btn {
  width: 80px;
  height: 34px;
  border: none;
  background-color: #ea4a36;
  color: #fff;
  cursor: pointer;
}
</style>
